﻿Public Class FormExperienceAED
    Public empId, expId As Int16
    Dim emp As New EmployeeInfo
    Public exper As New ExperinceInfo

    'Clear all textbox
    Public Sub ClearText()

        For Each Clear As Control In GroupBox1.Controls
            If TypeOf Clear Is TextBox Then
                Clear.Text = ""
            End If
        Next
    End Sub
    'load Employee Name to ComboBox
    Private Sub LoadName()
        For Each obj As Object In GetEmployeeName.GetEmployee.Values
            cboEmp_Name.Items.Add(obj)
        Next
    End Sub

    Private Sub cboEmp_Name_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboEmp_Name.SelectedIndexChanged
        'get employee id when ComboBox SelectedIndexChanged
        Dim group As EmployeeInfo = CType(cboEmp_Name.SelectedItem, EmployeeInfo)
        empId = group.EmployeeId
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        ClearText()
        LoadName()


    End Sub



    
    Private Sub SaveUpdate()
        Try
            'Passing values from object in class to TextBox
            With exper
                .ExperinceId = expId
                .EmployeeId = empId
                .CompanyName = txtCompanyName.Text
                .Duration = txtDuration.Text
                .EmployeePosition = txtPosition.Text
                .Salary = txtSalary.Text
                .JobDescription = txtJobDecription.Text
                .Note = txtNote.Text
                .StartDate = txtStartDate.Text
                .EndDate = txtEndDate.Text


                If expId = Nothing Then
                    'Insert Experience

                    Experience.AddExperience(exper)

                Else
                    'Update Experience

                    Experience.UpdateExperience(exper)

                End If

            End With
            AddExperienceToDataGridView()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Close()
    End Sub
    Public Sub AddExperienceToDataGridView()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing vlues DataGridView in form experence
            With FormExperience.dgvExperience.CurrentRow
                .Cells("EMPLOYEE_NAME").Value = cboEmp_Name.Text
                .Cells("COMPANY_NAME").Value = exper.CompanyName
                .Cells("DURATION").Value = exper.Duration
                .Cells("EMPLOYEE_POSITION").Value = exper.EmployeePosition
                .Cells("SALARY").Value = exper.Salary
                .Cells("JOB_DESCRIPTION").Value = exper.JobDescription
                .Cells("NOTE").Value = exper.Note
                .Cells("START_DATE").Value = exper.StartDate
                .Cells("END_DATE").Value = exper.EndDate

                If expId = Nothing Then

                    .Cells("EXPERIENCE_ID").Value = exper.ExperinceId
                    RefreshData.DescendingRefresh(FormExperience.dgvExperience, 0)

                    FormExperience.dgvExperience.CurrentCell = FormExperience.dgvExperience.Rows(0).Cells(1)
                Else

                    FormExperience.dgvExperience.CurrentCell = FormExperience.dgvExperience.Rows(0).Cells(1)

                    Exit Sub
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click
        SaveUpdate()
    End Sub

    Private Sub FormExperienceAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
​
End Class